package com.maijinjie.admin.content.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.content.bean.VideoNewsBean;
import com.maijinjie.admin.content.dao.VideoNewsMapper;
import com.maijinjie.admin.content.field.VideoNewsFields;
import com.maijinjie.admin.content.service.VideoNewsService;
import com.maijinjie.common.service.impl.BaseServiceImpl;

/**
 * VideoNewsServiceImpl
 * @author 罗正杰
 **/
@Service("videoNewsService")
public class VideoNewsServiceImpl extends BaseServiceImpl implements VideoNewsService{
	@Resource
	VideoNewsMapper videoNewsMapper;
	
	@Override
	public VideoNewsBean getBeanById(Integer id) throws Exception{
		if(null==id || 0>=id){
			return null;
		} else {
			return (VideoNewsBean) getBeanById(videoNewsMapper, id);
		}
	}
	@Override
	public int count(Map<String, Object> params) throws Exception {
		return videoNewsMapper.count(params);
	}
	@Override
	public List<VideoNewsBean> list(Map<String, Object> params) throws Exception {
		return (List<VideoNewsBean>) videoNewsMapper.list(params);
	}
	@Override
	public StatusBean save(VideoNewsBean bean, String createBy, Integer createUserId) throws Exception {
		Date currDate = new Date();
		//TODO 处理业务
		if(bean != null){
			if(bean.getMarkPath()==null || "".equals(bean.getMarkPath())){
				return error("请上传视频封面");
			}
			if(bean.getVideoPath()==null || "".equals(bean.getVideoPath())){
				return error("请填写视频ID");
			}			
			bean.setCreateDate(currDate);
			bean.setCreateUserId(createUserId);
			bean.setLastUpdateDate(currDate);
			bean.setLastUpdateUserId(createUserId);
			if(0<videoNewsMapper.save(bean)){
				return success("操作成功！");
			}
		}
		
		return error("操作失败！");
	}
	@Override
	public StatusBean update(VideoNewsBean bean, String createBy, Integer createUserId) throws Exception {
		if(null!=bean){
			Date currDate = new Date();
			HashMap<String, Object> params = new HashMap<String, Object>();
			//TODO 处理业务
			if(bean.getMarkPath()==null || "".equals(bean.getMarkPath())){
				return error("请上传视频封面");
			}
			if(bean.getVideoPath()==null || "".equals(bean.getVideoPath())){
				return error("请填写视频ID");
			}	
			params.put(VideoNewsFields.id.toString(), bean.getId());
			params.put(VideoNewsFields.description.toString(), bean.getDescription());
			params.put(VideoNewsFields.markPath.toString(), bean.getMarkPath());
			params.put(VideoNewsFields.videoPath.toString(),bean.getVideoPath());
			params.put(VideoNewsFields.title.toString(), bean.getTitle());
			params.put(VideoNewsFields.lastUpdateDate.toString(), currDate);
			params.put(VideoNewsFields.lastUpdateUserId.toString(), createUserId);
			
			if(videoNewsMapper.update(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean deleteById(Integer id) throws Exception {
		if(null!=id && 0<id){
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(VideoNewsFields.id.toString(), id);
			if(videoNewsMapper.delete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public Pagination<VideoNewsBean> list(Pagination<VideoNewsBean> pagination) throws Exception {
		return (Pagination<VideoNewsBean>) getPagination(videoNewsMapper, pagination);
	}
	@Override
	public StatusBean batchDelete(String ids) throws Exception {
		if(null!=ids && !"".equals(ids.trim())) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(VideoNewsFields.id.toString(), ids);
			if(videoNewsMapper.batchDelete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public List<VideoNewsBean> listAll() throws Exception {
		return (List<VideoNewsBean>) videoNewsMapper.list(null);
	}
}
